<?php
/**
 * Created by PhpStorm.
 * Script Name: Index.php
 * Create: 2023/5/23 11:03
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace addons\demo\admin\controller;
use addons\demo\common\model\Article as ArticleM;
use app\admin\model\Admin as AdminM;
use app\constants\Common;

class Article extends Base
{
    /**
     * @var ArticleM
     */
    protected $model;

    public function __construct(){
        parent::__construct();
        $this->model = new ArticleM();
    }

    public function index(){
        if(request()->isPost()){
            $post_data = input('post.');
            $where = [
                ['company_id', '=', AdminM::getCompanyId($this->adminInfo)]
            ];
            !empty($post_data['search_key']) && $where[] = ['title', 'like', '%'.$post_data['search_key'].'%'];

            $query = $this->model->where($where);
            $total = $query->count();
            if ($total) {
                $list = $query->page($post_data['page'], $post_data['limit'])
                    ->order('id', 'desc')
                    ->select();
            } else {
                $list = [];
            }

            $this->success('success', '', ['total' => $total, 'list' => $list]);
        }

        $builder = new ListBuilder();
        $builder->setSearch([
            ['type' => 'text', 'name' => 'search_key', 'title' => '搜索词','placeholder' => '文章标题']
        ])
            ->addTopButton('addnew', ['title' => '新增文章'])
            ->addTableColumn(['title' => 'ID', 'field' => 'id', 'minWidth' => 70])
            ->addTableColumn(['title' => '标题', 'field' => 'title', 'minWidth' => 90])
            ->addTableColumn(['title' => '描述', 'field' => 'desc', 'minWidth' => 130])
            ->addTableColumn(['title' => '阅读量', 'field' => 'view_num', 'minWidth' => 70])
            ->addTableColumn(['title' => '创建时间', 'field' => 'create_time', 'minWidth' => 130])
            ->addTableColumn(['title' => '修改时间', 'field' => 'update_time', 'minWidth' => 130])
            ->addTableColumn(['title' => '状态', 'field' => 'status', 'type' => 'switch', 'options' => Common::status(), 'minWidth' => 80])
            ->addTableColumn(['title' => '操作', 'minWidth' => 130, 'type' => 'toolbar'])
            ->addRightButton('edit')
            ->addRightButton('delete');
        return $builder->show();
    }

    /**
     * 编辑
     */
    public function edit(){
        $id = input('id');
        $data = $this->model->find($id);
        if(! $data){
            $this->error('id参数错误');
        }
        //使用FormBuilder快速建立表单页面。
        $builder = new FormBuilder();
        $builder->setMetaTitle('编辑')  //设置页面标题
        ->setPostUrl(addon_url('admin/article/savepost')) //设置表单提交地址
        ->addFormItem('id', 'hidden', 'id', 'id')
            ->addFormItem('title', 'text', '标题', '标题', [], 'required maxlength=50')
            ->addFormItem('desc', 'textarea', '简介', '不超过200字', [], 'required maxlength=200')
            ->addFormItem('content', 'ueditor', '内容', '内容')
            ->addFormItem('status', 'radio', '状态', '状态', [1 => '启用', 0 => '禁用'])
            ->setFormData($data);
        return $builder->show();
    }


    public function add(){
        //使用FormBuilder快速建立表单页面。
        $builder = new FormBuilder();
        $builder->setMetaTitle('新增')  //设置页面标题
        ->setPostUrl(addon_url('admin/article/savepost')) //设置表单提交地址
        ->addFormItem('title', 'text', '标题', '标题', [], 'required maxlength=50')
            ->addFormItem('desc', 'textarea', '简介', '不超过200字', [], 'required maxlength=200')
            ->addFormItem('content', 'ueditor', '内容', '内容');
        return $builder->show();
    }
}